package top.roadvast.rabbitmq.provider.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import top.roadvast.rabbitmq.utils.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 生产者，模拟为商品服务
 */
public class TopicSend {
    private final static String EXCHANGE_NAME = "topic_exchange";
 
    public static void main(String[] arg) {
        // 获取到连接
        // 获取通道
        try(Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();) {
            // 声明exchange，指定类型为topic
            channel.exchangeDeclare(EXCHANGE_NAME, "topic");
            // 消息内容
            String message = "新增商品 : id = 1001";
            // 发送消息，并且指定routing key 为：insert ,代表新增商品
            channel.basicPublish(EXCHANGE_NAME, "item.update", null, message.getBytes());
            System.out.println(" [商品服务：] Sent '" + message + "'");
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        }
    }
}